#include "Precompile.h"
#include "DX11IndexBuffer.h"
#include "DX11RenderSystem.h"

namespace Graphics{
	DX11IndexBuffer::DX11IndexBuffer(IndexDataPtr inds, RenderContext* handle):IndexBuffer(inds,handle),m_buffer(NULL){
		DX11RenderContext* ctx = (DX11RenderContext*)handle;

		// Fill in a buffer description.
		D3D11_BUFFER_DESC bufferDesc;
		bufferDesc.Usage            = D3D11_USAGE_DEFAULT;
		bufferDesc.ByteWidth        = inds->m_byteSize * inds->m_numInds;
		bufferDesc.BindFlags        = D3D11_BIND_INDEX_BUFFER;
		bufferDesc.CPUAccessFlags   = 0;
		bufferDesc.MiscFlags        = 0;

		// Fill in the subresource data.
		D3D11_SUBRESOURCE_DATA InitData;
		InitData.pSysMem = inds->m_data;
		InitData.SysMemPitch = 0;
		InitData.SysMemSlicePitch = 0;

		HRESULT hr = ctx->m_handle->CreateBuffer(&bufferDesc,&InitData,&m_buffer);
		if(FAILED(hr)){
			LOG_ERROR("DX11IndexBuffer - Failed in creating index buffer.");
			assert(0);
		}
	}

	DX11IndexBuffer::~DX11IndexBuffer(){
		if(m_buffer)
			m_buffer->Release();
	}

	void DX11IndexBuffer::setBuffer(void* context){
		DX11RenderContext *ctx = (DX11RenderContext*)context;

		UINT offset = 0;
		ctx->m_context->IASetIndexBuffer(m_buffer,DXGI_FORMAT_R32_UINT,offset);
	}
}